package com.yuanli.yuanliutils.yl_pangolin.holder;

import android.content.Context;

import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTCustomController;
import com.yuanli.yuanliutils.yl_pangolin.PangolinBean;

public class TTAdManagerHolder {
    private static boolean sInit;
    private static int isOpen;

    public static TTAdManager get() {
        if (!sInit) { throw new RuntimeException("TTAdSdk is not init, please check."); }
        return TTAdSdk.getAdManager();
    }

    private static PangolinBean mPangolinBean;

    public static PangolinBean getPangolinBean() {
        if (mPangolinBean == null){
            throw new RuntimeException("constants is empty");
        }
        return mPangolinBean;
    }

    public static void init(Context context, PangolinBean pangolinBean) {
        doInit(context);
        mPangolinBean = pangolinBean;
    }

    public static void setIsOpen(int isOpen){
        TTAdManagerHolder.isOpen = isOpen;
    }

    public static int getIsOpen(){
        return isOpen;
    }

    //step1:接入网盟广告sdk的初始化操作，详情见接入文档和穿山甲平台说明
    private static void doInit(Context context) {
        if (!sInit) {
            TTAdSdk.init(context, buildConfig());
            TTAdSdk.start(new TTAdSdk.Callback() {
                @Override
                public void success() {

                }

                @Override
                public void fail(int i, String s) {

                }
            });
            sInit = true;
        }
    }

    private static TTAdConfig buildConfig() {
        return new TTAdConfig.Builder()
                .appId(mPangolinBean.AD_APP_ID)
                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                .appName(mPangolinBean.AD_APP_NAME)
                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
                .allowShowNotify(true) //是否允许sdk展示通知栏提示
                .debug(true) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
                .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_4G) //允许直接下载的网络状态集合
                .supportMultiProcess(false)//是否支持多进程
                .customController(new TTCustomController() {
                    @Override
                    public boolean isCanUseLocation() {
                        return false;
                    }

                    @Override
                    public boolean isCanUsePhoneState() {
                        return false;
                    }

                    @Override
                    public boolean isCanUseWifiState() {
                        return false;
                    }

                    @Override
                    public boolean isCanUseWriteExternal() {
                        return false;
                    }

                    @Override
                    public boolean isCanUseAndroidId() {
                        return false;
                    }

                    @Override
                    public boolean isCanUsePermissionRecordAudio() {
                        return false;
                    }
                })
                //.httpStack(new MyOkStack3())//自定义网络库，demo中给出了okhttp3版本的样例，其余请自行开发或者咨询工作人员。
                .build();
    }
}
